import { useEffect } from 'react';
import { axhttp } from '@/shared/axhttp';
import { axurl } from '@/shared/axurl';
import { axhelper } from '@/shared/axhelper';
import { TokenLoading } from '@/components/PageLoading';

const Comp = () => {
  useEffect(() => {
    const token = axhelper.getQueryString('token');
    if (token && token != '') {
      axhttp.get(axurl.resource.menu).then(r => {
        if (r && r.success === true) {
          const data = r.data.find(i => i.id === '9002')?.children || [];
          localStorage.setItem('ax-data-menu', JSON.stringify(data));
          // let url = `${location.origin}/?t=${axhelper.getQueryString('t')}`;
          let url = `${location.origin}/`;
          const router = localStorage.getItem('ax-auth-router');
          if (router) {
            url += `#${router}`;
            localStorage.removeItem('ax-auth-router');
          }
          location.href = url;
        } else {
          localStorage.setItem('ax-data-menu', null);
          axhelper.reLogin();
        }
      });
      localStorage.setItem('ax-auth-token', `Bearer ${token}`);
    } else {
      localStorage.setItem('ax-auth-token', null);
      axhelper.reLogin();
    }
  }, []);

  return <TokenLoading />;
};

export default Comp;
